package hn.cch.grammar.java;

import org.junit.jupiter.api.Test;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class QueueDemo {


    /**
     * BlockingQueue<E>线程安全
     *
     * @throws InterruptedException
     */
    @Test
    public void testBlockingQueue() throws InterruptedException {
        BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>(1);

        // 抛异常
        boolean add = blockingQueue.add("A");
        String remove = blockingQueue.remove();
        // 返回值
        boolean offer = blockingQueue.offer("B");
        String poll = blockingQueue.poll();
        // 阻塞
        blockingQueue.put("C");
        String take = blockingQueue.take();
        // 等待
        boolean offerWait = blockingQueue.offer("D", 10, TimeUnit.SECONDS);
        String pollWait = blockingQueue.poll(10, TimeUnit.SECONDS);

    }


}
